<?php

namespace App\Http\Controllers\admin\notice;

use App\Http\Controllers\Controller;
use App\Jobs\NewsSendQueue;
use App\Models\admin\AdminUserModel;
use App\Models\news\NewsTemplateModel;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use lxs\api\ApiResponse;
use lxs\tools\Tools;

class NewsController extends Controller
{

    /**
     * 获取消息模版列表页面数据
     */
    public function getNewsTmplateListData(): JsonResponse
    {
        $data = request()->input();

        $query = NewsTemplateModel::with(['getOperator'])->where('deleted_at', 0);

        if ($data['class_type'] > 0) {
            $query->where('class_type', $data['class_type']);
        }

        if ($data['receive_type'] > 0) {
            $query->where('receive_type', $data['receive_type']);
        }

        if ($data['admin_id'] > 0) {
            $query->where('operator_id', $data['admin_id']);
        }

        if ($data['status'] != '' && $data['status'] >= 0) {
            $query->where('status', $data['status']);
        }

        $lists = $query->orderBy('id', 'desc')->paginate($data['pageSize']);
        foreach ($lists as $k => $v) {
            $lists[$k]['class_name'] = NewsTemplateModel::getClassValues($v['class_type']);
            $lists[$k]['receive_name'] = NewsTemplateModel::getReceiveValues($v['receive_type']);
            // 是否有编辑
            if ( $v['status'] == 0 ) {
                $lists[$k]['can_edit'] = 1;
            } else {
                $lists[$k]['can_edit'] = 0;
            }
        }

        $classTypeData = NewsTemplateModel::getClassValues();
        $receiveTypeData = NewsTemplateModel::getReceiveValues();
        $adminData = AdminUserModel::get();
        $statusData = NewsTemplateModel::getStatusValues();

        return ApiResponse::apiResponseData(200, '获取商品列表数据成功', [
            'lists' => $lists,
            'classTypeData' => $classTypeData,
            'receiveTypeData' => $receiveTypeData,
            'adminData' => $adminData,
            'statusData' => $statusData,
        ]);
    }


    /**
     * 获取消息模版详细页面数据
     */
    public function getNewsTmplateInfoData(): JsonResponse
    {
        $id = request()->input('id');

        $list = NewsTemplateModel::where('id', $id)->first();
        if ($list) {
            $list->toArray();
            $list['send_time'] = date('Y-m-d H:i:s', $list['send_time']);
            $list['class_type'] = NewsTemplateModel::getClassValues($list['class_type']);
            $list['receive_type'] = NewsTemplateModel::getReceiveValues($list['receive_type']);
            $list['status'] = NewsTemplateModel::getStatusValues($list['status']);
            $list['send_type'] = NewsTemplateModel::getSendTypeValues($list['send_type']);
            $list['nickname'] = AdminUserModel::where('id', $list['operator_id'])->value('nickname');
            $list['content'] = explode(',', $list['content']);
        }
        return ApiResponse::apiResponseData(200, '获取商品详细数据成功', [
            'list' => $list
        ]);
    }


    /**
     * 消息模版删除表单提交
     */
    public function newsTemplateDeleteSubmit(): JsonResponse
    {
        $id = request()->input('id');

        $res = NewsTemplateModel::where('id', $id)->update(['deleted_at' => time()]);
        if ($res) {
            return ApiResponse::apiResponseData(200, '删除成功');
        } else {
            return ApiResponse::apiResponseData(400, '删除失败');
        }
    }


    /**
     * 获取消息模版表单数据
     */
    public function getNewsTmplateFormData(): JsonResponse
    {
        $id = request()->input('id');
        $list = [];
        if ($id) {
            $list = NewsTemplateModel::find($id)->toArray();
            if ($list) {
                $list['send_time'] = date('Y-m-d H:i:s', $list['send_time']);
                $list['class_type'] = NewsTemplateModel::getClassValues($list['class_type']);
                $list['receive_type'] = NewsTemplateModel::getReceiveValues($list['receive_type']);
                $list['content'] = explode(',', $list['content']);
                $list['content_data'] = [];
                foreach ($list['content'] as $k => $v) {
                    $list['content_data'][$k]['url'][] = $v;
                }
            }
        }

        $classTypeData = NewsTemplateModel::getClassValues();
        $receiveTypeData = NewsTemplateModel::getReceiveValues();

        return ApiResponse::apiResponseData(200, '获取消息表单数据成功', [
            'lists' => $list,
            'classTypeData' => $classTypeData,
            'receiveTypeData' => $receiveTypeData,
        ]);
    }


    /**
     * 消息模版表单提交
     */
    public function newsTmplateFormSubmit(): JsonResponse
    {
        $post = request()->input();
        $post['operator_id'] = request()->system_uid;

        if ($post['content']) {
            $post['content'] = implode(',', $post['content']);
        }

        try {
            DB::beginTransaction();
            if (isset($post['id']) && $post['id'] > 0) {
                $newsInfo = NewsTemplateModel::find($post['id']);
                unset($post['class_type']);
                unset($post['receive_type']);
            } else {
                $newsInfo = new NewsTemplateModel();
            }

            if ($post['send_type'] == 1) {
                $post['send_time'] = time();
                $post['status'] = 1;
            } else {
                $post['send_time'] = Tools::timeConvert($post['send_time']);
            }

            $newsInfo->fill($post);
            $newsInfo->save();

            if (isset($newsInfo->id) && $post['send_type'] == 1) {
                NewsSendQueue::dispatch($newsInfo->id);
            }

            DB::commit();
            return ApiResponse::apiResponseData(200, '提交成功');
        } catch (\Exception $e) {
            DB::rollBack();
            Log::error(date('Y-m-d h:i:s',time()).' 消息表单提交失败:'.$e->getMessage());
            return ApiResponse::apiResponseData(500, '提交失败');
        }
    }

}
